package inet.app.vod.db;

import inet.app.vod.object.FunModel;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.inet.app.framework.util.Log;

public class DaoFun extends DaoBase {

	private ContentValues createContentValueFromRow(FunModel row) {

		ContentValues value = new ContentValues();
		value.put(DBConfig.FUN_ID, row.getId());
		value.put(DBConfig.FUN_TITLE, row.getTitle());
		value.put(DBConfig.FUN_VIEW_NUM, row.getViewNo());
		value.put(DBConfig.FUN_IMG, row.getImg());
		value.put(DBConfig.FUN_FB_LINK, row.getFbLink());
		return value;
	}

	private FunModel createRowFromCursor(Cursor cursor) {
		FunModel row = new FunModel();
		row.setId(cursor.getString(cursor.getColumnIndex(DBConfig.FUN_ID)));
		row.setTitle(cursor.getString(cursor.getColumnIndex(DBConfig.FUN_TITLE)));
		row.setViewNo(cursor.getInt(cursor
				.getColumnIndex(DBConfig.FUN_VIEW_NUM)));
		row.setImg(cursor.getString(cursor.getColumnIndex(DBConfig.FUN_IMG)));
		row.setFbLink(cursor.getString(cursor
				.getColumnIndex(DBConfig.FUN_FB_LINK)));
		return row;
	}

	public DaoFun(Context context) {
		super(context);
	}

	public long insertRow(FunModel row) {
		ContentValues value = createContentValueFromRow(row);
		return insertRow(DBConfig.TABLE_FUN, value);
	}

	public long insertCollection(List<FunModel> rows) {
		List<ContentValues> values = new ArrayList<ContentValues>();
		for (FunModel row : rows) {
			ContentValues value = createContentValueFromRow(row);
			values.add(value);
		}
		return insertCollection(DBConfig.TABLE_FUN, values);
	}

	public int deleteAll() {
		return deleteAll(DBConfig.TABLE_FUN);
	}

	public int deleteRow(String id) {
		int res = -1;
		if (!dbConnection.openSession())
			return res;
		Log.d(tag, "deleting row");
		Cursor cursor = dbConnection.query(DBConfig.TABLE_FUN, null,
				DBConfig.FUN_ID + "=?", new String[] { id + "" }, null, null,
				null);

		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then delete row */
				res = dbConnection.delete(DBConfig.TABLE_FUN, DBConfig.FUN_ID
						+ "=?", new String[] { id + "" });
			} else {
				/** row is not existed */
				res = 0;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	/**
	 * to update a row
	 * 
	 * @param row
	 * @return
	 */
	public int updateRow(FunModel row) {
		int res = -1;
		FunModel old = selectRow(row.getId());
		if (old == null)
			return res;
		Log.d("updating row");

		Cursor cursor = dbConnection.query(DBConfig.TABLE_FUN, null,
				DBConfig.FUN_ID + "=?", new String[] { row.getId() + "" },
				null, null, null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then update row */
				ContentValues value = createContentValueFromRow(row);
				res = dbConnection.update(DBConfig.TABLE_FUN, value,
						DBConfig.FUN_ID + "=?",
						new String[] { row.getId() + "" });
			} else {
				/** row is not existed */
				res = -1;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	public FunModel selectRow(String id) {
		FunModel row = null;
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return row;
			Log.d("selecting row");
			cursor = dbConnection.query(DBConfig.TABLE_FUN, null,
					DBConfig.FUN_ID + "=?", new String[] { id + "" }, null,
					null, null);

			if (cursor != null) {
				int rows = cursor.getCount();
				if (rows > 0) {
					/** row is existed, then get row info */
					cursor.moveToFirst();
					row = createRowFromCursor(cursor);
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			// String test = "1";
			dbConnection.closeSession();
		}
		return row;
	}

	public List<FunModel> selectAll() {

		List<FunModel> rows = new ArrayList<FunModel>();
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return rows;
			cursor = dbConnection.query(DBConfig.TABLE_FUN, null, null, null,
					null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						FunModel row = createRowFromCursor(cursor);
						rows.add(row);

					} while (cursor.moveToNext());
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			// String test = "1";
			dbConnection.closeSession();
		}
		return rows;
	}

}
